Shader "Shader Forge/OldShool" {
    Properties {
        _MainCol        ("颜色", color) = (1.0, 1.0, 1.0, 1.0)
        _SpecularPow    ("高光次幂", range(1, 90)) = 30
    }
    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_instancing
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0

            uniform float3 _MainCol;
            uniform float _SpecularPow;

            struct VertexInput {
                float4 vertex : POSITION;   // 模型顶点信息
                float4 normal : NORMAL;     // 法线信息
            };
            struct VertexOutput {
                float4 posCS : SV_POSITION;   // 由模型顶点信息换算而来的顶点屏幕位置
                float4 posWS : TEXCOORD0;
                float3 nDirWS : TEXTCOORD1; // 由模型发现信息换算来的世界空间法线信息
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;   // 新建一个输出结构
                o.posCS = UnityObjectToClipPos( v.vertex );   // 变换顶点信息并赋值
                o.posWS = mul(unity_ObjectToWorld, v.vertex);
                o.nDirWS = UnityObjectToWorldNormal(v.normal);  // 变换法线信息并赋值
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                // 准备向量
                float3 nDir = normalize(i.nDirWS);             // 获取nDir
                float3 lDir = normalize(_WorldSpaceLightPos0.xyz); // 获取lDir
                float3 vDir = normalize(_WorldSpaceCameraPos.xyz - i.posWS);  // vDir
                float3 hDir = normalize(vDir + lDir);
                float3 rDir = reflect(-lDir, nDir);

                // 准备点击结果
                float nDotl = dot(nDir, lDir);  // nDir dot lDir
                float nDoth = dot(nDir, hDir);
                float vDotr = dot(vDir, rDir);

                // 光照模型
                float lambert = max(0.0, nDotl);    // 截断赋值
                // float blinnPhong = pow(max(0.0, nDoth), _SpecularPow);
                // float3 finalRGB = _MainCol * lambert * + blinnPhong;
                
                float phong = pow(max(0.0, vDotr), _SpecularPow);
                float3 finalRGB = _MainCol * lambert * + phong;

                return float4(finalRGB, 1.0);   // 输出最终颜色
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}
